package com.wrx.club_management_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wrx.club_management_backend.common.AjaxResult;
import com.wrx.club_management_backend.entity.Notice;
import com.wrx.club_management_backend.service.NoticeService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @PostMapping("/push")
    public AjaxResult push(@RequestBody Notice notice){
        System.out.println(notice);
        boolean save = noticeService.save(notice);
        if(save) return AjaxResult.success("发布成功");
        else return AjaxResult.error("发布失败");
    }

    @PostMapping("/get/{stat}")
    public AjaxResult get(@PathVariable("stat") Long stat){
        List<Notice> list = noticeService.list(
                new LambdaQueryWrapper<Notice>()
                        .eq(Notice::getStat, stat)
        );
        return AjaxResult.success(list);
    }

    @PostMapping("/delete/{noticeId}")
    public AjaxResult delete(@PathVariable("noticeId") Long noticeId){
        boolean flag = noticeService.removeById(noticeId);
        if(flag) return AjaxResult.success("删除成功");
        else return AjaxResult.error("删除失败");
    }


}
